﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WoWAPI
{
	public partial class Guild
	{
		private const string Guild_URL = "http://{0}.battle.net/api/wow/guild/{1}/{2}";

		#region Properties
		public string Name { get; set; }
		public string Realm { get; set; }
		public int Level { get; set; }
		public int AchievementPoints { get; set; }
		public GuildEmblem Emblem { get; set; }
		public Achievements Achievements { get; set; }
		public Member[] Members { get; set; }
		public int Population { get; set; }
		public string Region { get; set; }
		public Faction Faction
		{
			get
			{
				return Common.GetFactionFromRace(this.Members[0].Character.Race);
			}
		}
		#endregion

		public Guild() { }

		public static Guild GetGuild(string GuildName, string Realm, string Region, params OptionalFields[] fields)
		{
			string url = String.Format(Guild_URL, Region, Realm, GuildName);

			if (fields.Length > 0)
			{
				StringBuilder sbParams = new StringBuilder();
				foreach (OptionalFields field in fields)
				{
					if (sbParams.Length == 0)
						sbParams.Append("?fields=");
					else
						sbParams.Append(",");

					sbParams.Append(field.ToString().ToLower());
				}
				url += sbParams.ToString();
			}

			System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
			Guild g = jss.Deserialize<Guild>(API.MakeWebRequest(url));
			g.Region = Region;
			if (g.Members != null)
				g.Population = g.Members.Length;
			return g;
		}

		#region Enums
		public enum OptionalFields
		{
			Members,
			Achievements
		}
		#endregion

		#region Sub Classes
		public class GuildEmblem
		{
			public int Icon { get; set; }
			public string IconColor { get; set; }
			public int Border { get; set; }
			public string BorderColor { get; set; }
			public string BackgroundColor { get; set; }

			public GuildEmblem() { }
		}
		#endregion
	}
}
